summaryrefslogtreecommitdiffstats
path: root/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/Settings.java
blob: 3126eba734a852de95c38bb40ce105e8aff9e1a4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package org.yuzu.yuzu_emu.features.settings.model;

import android.text.TextUtils;

import org.yuzu.yuzu_emu.YuzuApplication;
import org.yuzu.yuzu_emu.R;
import org.yuzu.yuzu_emu.features.settings.ui.SettingsActivityView;
import org.yuzu.yuzu_emu.features.settings.utils.SettingsFile;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class Settings {
    public static final String SECTION_GENERAL = "General";
    public static final String SECTION_SYSTEM = "System";
    public static final String SECTION_RENDERER = "Renderer";
    public static final String SECTION_AUDIO = "Audio";
    public static final String SECTION_CPU = "Cpu";

    private String gameId;

    private static final Map<String, List<String>> configFileSectionsMap = new HashMap<>();

    static {
        configFileSectionsMap.put(SettingsFile.FILE_NAME_CONFIG, Arrays.asList(SECTION_GENERAL, SECTION_SYSTEM, SECTION_RENDERER, SECTION_AUDIO, SECTION_CPU));
    }

    /**
     * A HashMap<String, SettingSection> that constructs a new SettingSection instead of returning null
     * when getting a key not already in the map
     */
    public static final class SettingsSectionMap extends HashMap<String, SettingSection> {
        @Override
        public SettingSection get(Object key) {
            if (!(key instanceof String)) {
                return null;
            }

            String stringKey = (String) key;

            if (!super.containsKey(stringKey)) {
                SettingSection section = new SettingSection(stringKey);
                super.put(stringKey, section);
                return section;
            }
            return super.get(key);
        }
    }

    private HashMap<String, SettingSection> sections = new Settings.SettingsSectionMap();

    public SettingSection getSection(String sectionName) {
        return sections.get(sectionName);
    }

    public boolean isEmpty() {
        return sections.isEmpty();
    }

    public HashMap<String, SettingSection> getSections() {
        return sections;
    }

    public void loadSettings(SettingsActivityView view) {
        sections = new Settings.SettingsSectionMap();
        loadCitraSettings(view);

        if (!TextUtils.isEmpty(gameId)) {
            loadCustomGameSettings(gameId, view);
        }
    }

    private void loadCitraSettings(SettingsActivityView view) {
        for (Map.Entry<String, List<String>> entry : configFileSectionsMap.entrySet()) {
            String fileName = entry.getKey();
            sections.putAll(SettingsFile.readFile(fileName, view));
        }
    }

    private void loadCustomGameSettings(String gameId, SettingsActivityView view) {
        // custom game settings
        mergeSections(SettingsFile.readCustomGameSettings(gameId, view));
    }

    private void mergeSections(HashMap<String, SettingSection> updatedSections) {
        for (Map.Entry<String, SettingSection> entry : updatedSections.entrySet()) {
            if (sections.containsKey(entry.getKey())) {
                SettingSection originalSection = sections.get(entry.getKey());
                SettingSection updatedSection = entry.getValue();
                originalSection.mergeSection(updatedSection);
            } else {
                sections.put(entry.getKey(), entry.getValue());
            }
        }
    }

    public void loadSettings(String gameId, SettingsActivityView view) {
        this.gameId = gameId;
        loadSettings(view);
    }

    public void saveSettings(SettingsActivityView view) {
        if (TextUtils.isEmpty(gameId)) {
            view.showToastMessage(YuzuApplication.getAppContext().getString(R.string.ini_saved), false);

            for (Map.Entry<String, List<String>> entry : configFileSectionsMap.entrySet()) {
                String fileName = entry.getKey();
                List<String> sectionNames = entry.getValue();
                TreeMap<String, SettingSection> iniSections = new TreeMap<>();
                for (String section : sectionNames) {
                    iniSections.put(section, sections.get(section));
                }

                SettingsFile.saveFile(fileName, iniSections, view);
            }
        } else {
            // custom game settings
            view.showToastMessage(YuzuApplication.getAppContext().getString(R.string.gameid_saved, gameId), false);

            SettingsFile.saveCustomGameSettings(gameId, sections);
        }

    }
}